<template>
  <div class="container" v-loading="status_loading">
    <el-row>
      <el-form :model="formInline" style="width:100%" @keyup.enter.native="search">
        <el-col :span="8" align="left">
          <el-form-item :label="$t('m.quality.qccode')" :label-width="formLabelWidth">
            <el-input maxlength="20" show-word-limit v-model="formInline.qccode"></el-input>
          </el-form-item>
          <el-form-item label="SKU" :label-width="formLabelWidth">
            <el-input maxlength="20" show-word-limit v-model="formInline.productspeccode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" align="left">
          <el-form-item :label="$t('m.sigin.noticenumber')" :label-width="formLabelWidth">
            <el-input maxlength="20" show-word-limit v-model="formInline.noticenumber"></el-input>
          </el-form-item>
          <el-form-item label="SupplierCode" :label-width="formLabelWidth">
            <el-input maxlength="20" show-word-limit v-model="formInline.suppliercode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" align="left">
          <el-form-item :label="$t('m.in.ordernumber')" :label-width="formLabelWidth">
            <el-input maxlength="30" show-word-limit v-model="formInline.ordernumber"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" align="right">
          <el-button type="primary" @click="search" v-if="hasPermission('search')">{{$t('m.query')}}</el-button>
          <el-button v-if="hasPermission('takeGoods')" @click="takeGoods">{{$t('m.quality.takeGoods')}}</el-button>
          <el-button v-if="hasPermission('print')" @click="handlePrint">{{$t('m.print')}} SKU</el-button>
          <el-button type="primary" @click="qualityTest" v-if="hasPermission('quality')">{{$t('m.quality.qc')}}</el-button>
        </el-col>
      </el-form>
    </el-row>
    <div class="ellipse"></div>
    <el-table ref="table" stripe empty-text fits class="table-wrap single-select-table" :data="tableData" :row-style="tableRowStyle" :row-class-name="tableRowClassName" @row-click="rowClick" @selection-change="handleSingleSelectionChange" v-loading="tableLoading">
      <el-table-column align="center" type="selection" width="55"></el-table-column>
      <el-table-column align="center" type="index" :label="$t('m.index')" width="50"></el-table-column>
      <el-table-column align="center" prop="qccode" :label="$t('m.quality.qccode')">
        <template slot-scope="scope">
          <el-button type="text" @click="qualityResult(scope.row)">{{scope.row.qccode}}</el-button>
        </template>
      </el-table-column>
      <el-table-column :label="$t('m.state')" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.state==1">{{$t('m.quality.state1')}}</span>
          <span v-if="scope.row.state==2">{{$t('m.quality.state2')}}</span>
          <span v-if="scope.row.state==3">{{$t('m.quality.state3')}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="signintasknumber" :label="$t('m.sigin.tasknumber')" width="200"></el-table-column>
      <el-table-column align="center" prop="tsareacode" :label="$t('m.sigin.tsareacode')"></el-table-column>
      <el-table-column align="center" prop="isqualitycontrol" :label="$t('m.quality.isqualitycontrol')">
        <template slot-scope="scope">
          <span v-if="scope.row.isqualitycontrol==1">{{$t('m.yes')}}</span>
          <span v-if="scope.row.isqualitycontrol==0">{{$t('m.no')}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="noticenumber" :label="$t('m.sigin.noticenumber')"></el-table-column>
      <el-table-column align="center" prop="receiving" :label="$t('m.quality.receiving')"></el-table-column>
      <el-table-column align="center" prop="receivingdate" :label="$t('m.quality.receivingdate')"></el-table-column>
    </el-table>

    <el-pagination background layout="sizes, prev, pager, next, jumper" :page-sizes="pageSizeList" :page-size="rowSize" :current-page="page" :total="totalRows" @current-change="currentChangeInitFn" @size-change="handleSizeChangeInitFn"></el-pagination>

    <!-- 质检结果 dialog 开始 -->
    <el-dialog @close="close" width="1200px" :title="$t('m.quality.result')" :visible.sync="dialogFormVisible">
      <el-form align="left" ref="formQualityResult" :model="formQualityResult">
        <el-row>
          <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item size="large" :label="$t('m.quality.qccode')" :label-width="formLabelWidth" prop="qccode">
              <el-input disabled v-model="formQualityResult.qccode" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="6" :sm="6" :md="6" :lg="6" :xl="6">
            <el-form-item size="large" :label="$t('m.in.receive')" :label-width="formLabelWidth" prop="receivingqty">
              <el-input disabled v-model="formQualityResult.receivingqty" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="6" :sm="6" :md="6" :lg="6" :xl="6">
            <el-form-item size="large" :label="$t('m.state')" :label-width="formLabelWidth" prop="state">
              <span v-if="formQualityResult.state==1">{{$t('m.quality.state1')}}</span>
              <span v-if="formQualityResult.state==2">{{$t('m.quality.state2')}}</span>
              <span v-if="formQualityResult.state==3">{{$t('m.quality.state3')}}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item :label="$t('m.quality.tasknumber')" :label-width="formLabelWidth" prop="qccode" size="large">
              <el-input disabled v-model="formQualityResult.signintasknumber" autocomplete="off"></el-input>
            </el-form-item>
            <el-row>
              <el-col :span="10">
                <el-form-item :label="$t('m.quality.passedqty')" :label-width="formLabelWidth" prop="passedqty" size="large">
                  <el-input disabled v-model="formQualityResult.passedqty" autocomplete="off"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="14">
                <el-form-item :label="$t('m.page.location')+$t('m.code')" :label-width="formLabelWidth" prop="passedlocationcode" size="large">
                  <el-input disabled v-model="formQualityResult.passedlocationcode" autocomplete="off"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item :label="$t('m.page.product')+$t('m.code')" :label-width="formLabelWidth" prop="productspeccode" size="large">
              <el-input disabled v-model="formQualityResult.productspeccode" autocomplete="off"></el-input>
            </el-form-item>
            <el-row>
              <el-col :span="10">
                <el-form-item :label="$t('m.quality.unqualifiedqty')" :label-width="formLabelWidth" prop="unqualifiedqty" size="large">
                  <el-input disabled v-model="formQualityResult.unqualifiedqty" autocomplete="off"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="14">
                <el-form-item :label="$t('m.page.location')+$t('m.code')" :label-width="formLabelWidth" prop="unqualifiedlocationcode">
                  <el-input disabled v-model="formQualityResult.unqualifiedlocationcode" autocomplete="off"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="8" v-for="(item,index) in formQualityResult.batchattr" :key="index">
            <el-form-item :label="item.label" :label-width="formLabelWidth">
              <el-input disabled size="mini" v-model="item.value" autocomplete="off"></el-input>
            </el-form-item>
            <!-- {{item}} -->
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('m.remark')" :label-width="formLabelWidth" prop="productspeccode" size="large">
              <el-input type="textarea" disabled v-model="formQualityResult.remark" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="btns">
        <el-button size="mini" @click="close">{{$t('m.close')}}</el-button>
      </div>
    </el-dialog>
    <!-- 质检结果 dialog 结束 -->

    <!-- 质检 dialog 开始 -->
    <el-dialog :close-on-click-modal="false" @close="closeTestDialog" width="1200px" :title="$t('m.quality.qc')" :visible.sync="dialogFormVisible1">
      <el-form align="left" ref="formQualityTest" size :model="formQualityTest">
        <el-row>
          <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item :label="$t('m.quality.tasknumber')" :label-width="formLabelWidth" prop="signintasknumber" size="large">
              <el-input maxlength="20" show-word-limit @input="func()" @blur="blur&&getTestInfoBlur()" @keyup.enter.native="getTestInfoEnter" v-model="formQualityTest.signintasknumber" autocomplete="off"></el-input>
            </el-form-item>
            <el-row>
              <el-col :span="11">
                <el-form-item :label="$t('m.quality.passedqty')" :label-width="formLabelWidth" prop="passedqty" size="large">
                  <el-input maxlength="6" show-word-limit @input="getpassedqty;validateNumber('passedqty')" v-model.number="formQualityTest.passedqty" autocomplete="off"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="13">
                <el-form-item :label="$t('m.page.location')+$t('m.code')" :label-width="formLabelWidth" prop="passedlocationcode" size="large">
                  <el-input :disabled="formQualityTest.passedqty?false:true" @focus="showLocationList(1)" v-model="formQualityTest.passedlocationcode"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item :label="$t('m.page.product')+$t('m.code')" :label-width="formLabelWidth" prop="productspeccode" size="large">
              <el-input disabled v-model="formQualityTest.productspeccode" autocomplete="off"></el-input>
            </el-form-item>
            <el-row>
              <el-col :span="11">
                <el-form-item :label="$t('m.quality.unqualifiedqty')" :label-width="formLabelWidth" prop="unqualifiedqty" size="large">
                  <el-input maxlength="6" show-word-limit @input="getunpassedqty;validateNumber('unqualifiedqty')" v-model.number="formQualityTest.unqualifiedqty" autocomplete="off"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="13">
                <el-form-item :label="$t('m.page.location')+$t('m.code')" :label-width="formLabelWidth" prop="unqualifiedlocationcode" size="large">
                  <el-input :disabled="formQualityTest.unqualifiedqty?false:true" @focus="showLocationList(0)" v-model="formQualityTest.unqualifiedlocationcode"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="8" v-for="(item,index) in batchattr" :key="index">
            <el-form-item v-if="item.label=='保质期'||item.label=='入库日期'" :label="item.label" :label-width="formLabelWidth">
              <el-input disabled size="mini" v-model="item.value" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item v-else-if="item.label=='截至日期'" :label="item.label" :label-width="formLabelWidth">
              <!-- value-format="timestamp" -->
              <el-date-picker @change="calculationDate1" type="date" v-model="item.value"></el-date-picker>
            </el-form-item>
            <el-form-item v-else-if="item.label=='生产日期'" :label="item.label" :label-width="formLabelWidth">
              <el-date-picker @change="calculationDate2" type="date" v-model="item.value"></el-date-picker>
            </el-form-item>
            <el-form-item v-else :label="item.label" :label-width="formLabelWidth">
              <el-input :disabled="(item.inputtype===3||item.inputtype===4)?true:false" size="mini" v-model="item.value" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="btns">
        <el-button size="mini" @click="saveGood">{{$t('m.save')}}</el-button>
        <el-button size="mini" @click="settingComplete">{{$t('m.quality.setting')}}</el-button>
        <el-button size="mini" @click="closeTestDialog">{{$t('m.close')}}</el-button>
      </div>
    </el-dialog>
    <!-- 质检 dialog 结束 -->

    <!-- 取货 dialog 开始 -->
    <el-dialog :close-on-click-modal="false" @close="closeFirstDialog" width="1200px" :title="$t('m.quality.takeGoods')" :visible.sync="dialogFormVisible2">
      <el-form align="left" ref="formTakeGoods" size :model="formTakeGoods">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('m.quality.tasknumber')" prop="signintasknumber" size="large">
              <el-input maxlength="20" show-word-limit @input="func2" @blur="blur2&&getGoodInfoBlur()" @keyup.enter.native="getGoodInfoEnter" v-model="formTakeGoods.signintasknumber" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <template>
          <el-tabs v-model="activeName" type="card">
            <el-tab-pane :label="$t('m.quality.take1')" name="first">
              <el-table :data="goodInfo" stripe style="width: 100%;margin-bottom:30px;">
                <el-table-column align="center" type="index" :label="$t('m.index')" width="50"></el-table-column>
                <el-table-column align="center" prop="productspeccode" label="SKU"></el-table-column>
                <el-table-column align="center" prop="suppliercode" label="SupplierCode"></el-table-column>
                <el-table-column align="center" prop="productname" :label="$t('m.in.productname')"></el-table-column>
                <el-table-column align="center" prop="productspec" :label="$t('m.in.productspec')"></el-table-column>
                <el-table-column align="center" prop="signquantity" :label="$t('m.quality.count')"></el-table-column>
              </el-table>
            </el-tab-pane>
            <el-tab-pane :label="$t('m.quality.take2')" name="second">{{$t('m.quality.take2')}}</el-tab-pane>
          </el-tabs>
        </template>
        <el-form-item :label="$t('m.quality.receiving')" prop="receivingid" :rules="[{ required: true, message: $t('m.choose')+$t('m.quality.receiving'), trigger: 'change'  }]">
          <el-select @change="receiveInfo" size="mini" v-model="formTakeGoods.receivingid" :placeholder="$t('m.choose')">
            <el-option v-for="item in receiver" :key="item.id" :label="item.username" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <div class="btns">
          <el-button size="mini" @click="takeComfirm">{{$t('m.quality.confirm')}}</el-button>
          <el-button size="mini" @click="closeFirstDialog">{{$t('m.close')}}</el-button>
        </div>
      </el-form>
    </el-dialog>
    <!-- 取货 dialog 结束 -->

    <!-- 备注 dialog 开始 -->
    <el-dialog :close-on-click-modal="false" @close="closeRemark" width="600px" :title="$t('m.quality.setting')" :visible.sync="dialogFormVisible3">
      <el-form align="left" :rules="rulesRemark" ref="formRemark" size :model="formRemark">
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('m.remark')" prop="remark" size="large">
              <el-input type="textarea" maxlength="100" show-word-limit v-model="formRemark.remark" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="btns">
          <el-button size="mini" @click="forceSaveGood">{{$t('m.save')}}</el-button>
          <el-button size="mini" @click="closeRemark">{{$t('m.cancel')}}</el-button>
        </div>
      </el-form>
    </el-dialog>
    <!-- 备注 dialog 结束 -->

    <!-- 选择库位dialog start -->
    <locationListPopup v-if="locationListPopupVisible" :popupVisible="locationListPopupVisible" :config="locationConfig" @submit="setLocation" @close="locationListPopupVisible=false"></locationListPopup>
    <!-- 选择库位dialog end -->
  </div>
</template>

<script>
import db from '@/utils/db'
import common from '@/utils/common'
import { getLodop } from '@/utils/LodopFuncs'
export default {
  data(){
    return {
      saveParams:{
        productspeccode:'',
        passedqty:null,
        unqualifiedqty:null
      },
      opt:'', //给子组件的参数,判断是通过的库位还是未通过的库位
      locationConfig:{}, // 需要传递给locations-list组件的参数
      locationListPopupVisible:false, // 控制选择库位dialog显示隐藏
      blur:true,
      blur2:true,
      batchattr:[],
      status_loading:false,
      formInline:{
        qccode:'',
        productspeccode:'',
        noticenumber:'',
        suppliercode:'',
        ordernumber:''
      },
      signintasknumber:'',
      receivingqty:null,
      activeName:'first',
      tableData:[],
      dialogFormVisible:false,
      dialogFormVisible1:false,
      dialogFormVisible2:false,
      dialogFormVisible3:false,
      rulesRemark:{
        remark:[{ required:true, message:'请填写备注', trigger:'blur' }]
      },
      formRemark:{
        remark:''
      },
      formQualityResult:{
        arrivalnoticeid:null,
        batchattr:'',
        isqualitycontrol:null,
        noticenumber:'',
        ordernumber:'',
        passedlocationcode:'',
        passedlocationid:null,
        passedqty:null,
        productid:null,
        productname:'',
        productspec:'',
        productspeccode:'',
        qccode:'',
        qcdate:'',
        qcid:null,
        qcuser:'',
        qualitycontrolid:null,
        receiving:'',
        receivingdate:'',
        receivingid:null,
        receivingqty:null,
        signintasknumber:'',
        state:null,
        suppliercode:'',
        tasknumber:'',
        tsareacode:'',
        tsareaid:null,
        unqualifiedlocationcode:'',
        unqualifiedlocationid:null,
        unqualifiedqty:0,
        remark:''
      },
      formQualityTest:{
        arrivalnoticeid:null,
        batchattr:'',
        isqualitycontrol:null,
        noticenumber:'',
        ordernumber:'',
        passedlocationcode:'',
        passedlocationid:null,
        passedqty:null,
        productid:null,
        productname:'',
        productspec:'',
        productspeccode:'',
        qccode:'',
        qcdate:'',
        qcid:null,
        qcuser:'',
        qualitycontrolid:null,
        receiving:'',
        receivingdate:'',
        receivingid:null,
        receivingqty:null,
        signintasknumber:'',
        state:null,
        suppliercode:'',
        tasknumber:'',
        tsareacode:'',
        tsareaid:null,
        unqualifiedlocationcode:'',
        unqualifiedlocationid:null,
        unqualifiedqty:0,
        remark:''
      },
      formTakeGoods:{
        arrivalnoticeid:null,
        batchattr:'',
        isqualitycontrol:null,
        noticenumber:'',
        ordernumber:'',
        passedlocationcode:'',
        passedlocationid:null,
        passedqty:null,
        productid:null,
        productname:'',
        productspec:'',
        productspeccode:'',
        qccode:'',
        qcdate:'',
        qcid:null,
        qcuser:'',
        qualitycontrolid:null,
        receiving:'',
        receivingdate:'',
        receivingid:null,
        receivingqty:null,
        signintasknumber:'',
        state:null,
        suppliercode:'',
        tasknumber:'',
        tsareacode:'',
        tsareaid:null,
        unqualifiedlocationcode:'',
        unqualifiedlocationid:null,
        unqualifiedqty:0
      },
      goodInfo:[],
      receiver:[],
      formLabelWidth:'140px',
      location:[],
      unpasslocation:[],
      allProduct:null,
      receivername:'',
      receivingid:null,
      signquantity:null,
      remark:'',
      passedlocationcode:null,
      unqualifiedlocationcode:null,
      date1:'', //保质期
      noData:''
    }
  },
  components:{
    locationListPopup:() => import('@/components/common/location-list')
  },
  computed:{

  },
  methods:{
    validateNumber(key){
      this.$set(this.formQualityTest,key,this.formQualityTest[key].toString().replace(/[^\d]/g,''))
    },
    init(){
      this.tableLoading = true
      this.$axios({
        method:'get',
        url:'/qc/page',
        params:{
          page:this.page,
          rows:this.rowSize,
          qccode:this.formInline.qccode,
          productspeccode:this.formInline.productspeccode,
          noticenumber:this.formInline.noticenumber,
          suppliercode:this.formInline.suppliercode,
          ordernumber:this.formInline.ordernumber
        }
      }).then(({ data }) => {
        for (let i = 0;i < data.records.length;i++){
          data.records[
            i
          ].receivingdate = common.timestampToTime(
            data.records[i].receivingdate
          )
          if (
            data.records[i].batchattr === '[{}]' ||
            !data.records[i].batchattr
          )
            data.records[i].batchattr = '[]'
          data.records[i].batchattr = JSON.parse(
            data.records[i].batchattr
          )
        }

        this.tableData = data.records
        this.totalRows = data.total
        setTimeout(()=>this.tableLoading=false,500)
      })
    },
    search(){
      this.page = 1
      this.init()
    },
    close(){
      this.dialogFormVisible = false
      this.init()
    },
    closeTestDialog(){
      this.dialogFormVisible1 = false
      this.location = []
      this.unpasslocation = []
      this.formQualityTest.passedlocationid = null
      this.formQualityTest.unqualifiedlocationid = null
      this.$refs['formQualityTest'].resetFields()
      this.batchattr = ''
      this.init()
    },
    closeFirstDialog(){
      this.dialogFormVisible2 = false
      this.$refs['formTakeGoods'].resetFields()
      this.goodInfo = []
      this.init()
    },
    closeRemark(){
      this.dialogFormVisible3 = false
      this.$refs['formRemark'].resetFields()
    },
    qualityResult(row){
      this.formQualityResult = row
      this.formQualityResult.createdate = common.timestampToTime(
        this.formQualityResult.createdate
      )
      this.dialogFormVisible = true
    },
    qualityTest(){
      this.dialogFormVisible1 = true
    },
    takeGoods(){
      this.getReceiver()
      this.dialogFormVisible2 = true
    },
    func(){
      this.blur = true
    },
    getTestInfoBlur(){
      this.getTestInfo()
    },
    getTestInfoEnter(){
      this.blur = false
      this.getTestInfo()
    },
    getTestInfo(){
      let code = this.formQualityTest.signintasknumber.replace(/\s+/g, '')
      if (code){
        this.$axios({
          method:'get',
          url:`/qc/tasknumber?signintasknumber=${code}`
        }).then(({ data }) => {
          if (
            data.state === 1 &&
            data.isqualitycontrol === 1
          ){
            this.$message({
              showClose:true,
              message:this.$t('m.quality.step'),
              type:'warning',
              duration:1000
            })
            return
          }
          if (data.state === 3){
            this.$message({
              showClose:true,
              message:this.$t('m.quality.state3'),
              type:'warning',
              duration:1000
            })
            return
          } else {
            this.saveParams.productspeccode =
              data.productspeccode
            this.saveParams.passedqty = data.signquantity
            this.formQualityTest.productspeccode =
              data.productspeccode
            this.formQualityTest.passedqty = data.signquantity
            this.formQualityTest.unqualifiedqty = 0
            this.formQualityTest.isqualitycontrol =
              data.isqualitycontrol
            this.allProduct = data.signquantity
            if (
              data.batchattr === '[{}]' ||
              !data.batchattr
            )
              data.batchattr = '[]'
            this.batchattr = JSON.parse(data.batchattr)
            for (let i = 0;i < this.batchattr.length;i++){
              if (this.batchattr[i].label === '保质期'){
                this.date1 = this.batchattr[i].value
              }
            }
          }
        })
      }
    },
    func2(){
      this.blur2 = true
    },
    getGoodInfoBlur(){
      this.getGoodInfo()
    },
    getGoodInfoEnter(){
      this.blur2 = false
      this.getGoodInfo()
    },
    getGoodInfo(){
      let that = this
      this.$axios({
        method:'get',
        url:`/qc/tasknumber?signintasknumber=${this.formTakeGoods.signintasknumber}`
      }).then(({ data }) => {
        if (data.state === 2){
          that.$message({
            showClose:true,
            message:that.$t('m.quality.state2'),
            type:'warning',
            duration:1000
          })
          return
        }
        if (data.state === 3){
          that.$message({
            showClose:true,
            message:that.$t('m.quality.state3'),
            type:'warning',
            duration:1000
          })
          return
        } else {
          that.goodInfo = []
          that.goodInfo.push(data)
          that.signquantity = data.signquantity
        }
      })
    },
    getReceiver(){
      this.$axios({
        method:'get',
        url:`/user/role/listByname?rolename=质检员`
      }).then(({ data }) => {
        this.receiver = data
      })
    },
    takeComfirm(){
      this.$refs['formTakeGoods'].validate(valid => {
        if (valid){
          let that = this
          that
            .$axios({
              method:'post',
              url:'/qc/takegoods',
              params:{
                receiver:that.receivername,
                receivingid:that.receivingid,
                receivingqty:that.signquantity,
                tasknumber:that.formTakeGoods.signintasknumber
              }
            })
            .then(() => {
              that.$message({
                showClose:true,
                message:that.$t('m.quality.takesuccess'),
                type:'success',
                duration:1000
              })
              that.dialogFormVisible2 = false
              that.page = 1
              that.init()
            })
        }
      })
    },
    receiveInfo(id){
      this.receivingid = id
      this.receivername = this.receiver.filter(function (item){
        return item.id === id
      })[0].username
    },
    saveGood(){
      let that = this
      for (let i = 0;i < that.batchattr.length;i++){
        if (
          that.batchattr[i].label === '截至日期' ||
          that.batchattr[i].label === '生产日期'
        ){
          that.batchattr[i].value = common.timestampSimple(
            that.batchattr[i].value
          )
        }
        if (
          that.batchattr[i].format === 4
        ){
          that.batchattr[i].value = common.timestampToTime(
            that.batchattr[i].value
          )
        }
      }
      that.$refs['formQualityTest'].validate(valid => {
        if (valid){
          if (!that.formQualityTest.remark){
            if (
              Number(that.formQualityTest.passedqty) +
              Number(that.formQualityTest.unqualifiedqty) !==
              that.allProduct
            ){
              return that.$message({
                showClose:true,
                message:that.$t('m.quality.checkcount'),
                type:'warning',
                duration:1000
              })
            }
          }

          if (
            that.formQualityTest.passedqty &&
            !that.formQualityTest.passedlocationcode
          ){
            that.callbackMessage(
              that.$t('m.choose') + that.$t('m.page.location')
            )
            return false
          }
          if (
            that.formQualityTest.passedlocationcode === '' &&
            that.formQualityTest.passedqty !== 0
          ){
            return that.$message({
              showClose:true,
              message:that.$t('m.choose') + that.$t('m.page.location'),
              type:'warning',
              duration:1000
            })
          }
          if (
            that.formQualityTest.unqualifiedlocationcode === '' &&
            that.formQualityTest.unqualifiedqty !== 0
          ){
            return that.$message({
              showClose:true,
              message:that.$t('m.choose') + that.$t('m.page.location'),
              type:'warning',
              duration:1000
            })
          } else {
            that.formQualityTest.batchattr = JSON.stringify(that.batchattr)
            db.set('signintasknumber', that.formQualityTest.signintasknumber)
            that
              .$axios({
                method:'post',
                url:'/qc/check',
                data:that.formQualityTest
              })
              .then(({ data }) => {
                that.$message({
                  showClose:true,
                  message:that.$t('m.quality.success'),
                  type:'success',
                  duration:1000
                })
                that.dialogFormVisible1 = false
                that.print(data.qualitycontrolid)
                that.page = 1
                that.init()
                that.formQualityTest.remark = ''
              })
          }
        }
      })
    },
    print(id){
      let LODOP = getLodop()
      let obj = {
        ids:[id],
        templateName:'quality'
      }
      this.$axios({
        method:'post',
        url:'/printtemplate/query',
        data:obj
      }).then(({ data }) => {
        this.printContent = data
        eval(this.printContent)
        LODOP.SET_PRINT_PAGESIZE(1, '50mm', '30mm', '')
        LODOP.PREVIEW()
      })
    },
    handlePrint(){
      if (!this.tableRowSelected.length)
        return this.callbackMessage(`${this.$t('m.chooseprint')}sku`)
      let { signintasknumber } = this.tableRowSelected[0]
      this.$router.push({
        path:'quality/printSku',
        query:{ signintasknumber }
      })
    },
    settingComplete(){
      let that = this
      if (
        that.formQualityTest.passedqty &&
        !that.formQualityTest.passedlocationcode
      ){
        that.callbackMessage(that.$t('m.choose') + that.$t('m.page.location'))
        return false
      }
      if (
        that.formQualityTest.passedlocationcode === '' &&
        that.formQualityTest.passedqty !== 0
      ){
        return that.$message({
          showClose:true,
          message:that.$t('m.choose') + that.$t('m.page.location'),
          type:'warning',
          duration:1000
        })
      }
      if (
        that.formQualityTest.unqualifiedlocationcode === '' &&
        that.formQualityTest.unqualifiedqty !== 0
      ){
        return that.$message({
          showClose:true,
          message:that.$t('m.choose') + that.$t('m.page.location'),
          type:'warning',
          duration:1000
        })
      } else {
        that.dialogFormVisible3 = true
      }
    },
    //强制保存
    forceSaveGood(){
      let that = this
      that.$refs['formRemark'].validate(valid => {
        if (valid){
          if (
            Number(that.formQualityTest.passedqty) +
            Number(that.formQualityTest.unqualifiedqty) >
            that.allProduct
          ){
            return that.$message({
              showClose:true,
              message:that.$t('m.quality.checkcount'),
              type:'warning',
              duration:1000
            })
          } else {
            that.formQualityTest.remark = that.formRemark.remark
            that.dialogFormVisible3 = false
            that.saveGood()
          }
        }
      })
    },
    settingValid(){
      let that = this
      that.$refs['formRemark'].validate(valid => {
        if (valid){
          that.remark = that.formRemark.remark
          that
            .$axios({
              method:'get',
              url:'/qc/check',
              params:{
                batchattr:'string',
                passedlocationcode:'string',
                passedlocationid:0,
                passedqty:0,
                remark:'string',
                signintasknumber:'string',
                unqualifiedlocationcode:'string',
                unqualifiedlocationid:0,
                unqualifiedqty:0
              }
            })
            .then(({ data }) => {
              for (let i = 0;i < data.records.length;i++){
                data.records[
                  i
                ].arrivalnoticeDto.createdate = common.timestampToTime(
                  data.records[i].arrivalnoticeDto.createdate
                )
                data.records[
                  i
                ].arrivalnoticeDto.modifydate = common.timestampToTime(
                  data.records[i].arrivalnoticeDto.modifydate
                )
              }

              that.tableData = data.records
              that.total = data.total
              that.pageSize = data.size
              that.status_loading = false
            })
        }
      })
    },
    showLocationList(type){
      this.opt = type ? 'pass' : 'unpass'
      let sku = this.formQualityTest.productspeccode
      let quantity = type ? this.formQualityTest.passedqty : this.formQualityTest.unqualifiedqty
      let typelist = (type ? [1, 2, 3] : [4, 5]).join(',') // [1:收货区,2:存货区,3:拣货区,4:不良品区,5:退货区]
      this.locationConfig = {
        params:{ sku, quantity, typelist }
      }
      this.locationListPopupVisible = true
    },
    setLocation(location){
      let key = this.opt === 'pass' ? 'passedlocation' : 'unqualifiedlocation'
      this.formQualityTest[`${key}code`] = location.locationcode
      this.formQualityTest[`${key}id`] = location.locationid
      this.locationListPopupVisible = false
    },
    getpassedqty(val){
      this.saveParams.passedqty = val
    },
    getunpassedqty(val){
      this.saveParams.unqualifiedqty = val
    },
    //通过质检货物库位
    getPassCode(id){
      this.formQualityTest.passedlocationcode = this.location.filter(function (
        item
      ){
        return item.locationid == id
      })[0].locationcode
    },
    //未通过质检货物库位
    getUnpassCode(id){
      this.formQualityTest.unqualifiedlocationcode = this.unpasslocation.filter(
        function (item){
          return item.locationid == id
        }
      )[0].locationcode
    },
    calculationDate1(date){
      // this.batchattr=[{
      //   label:'生产日期',
      //   value:'2020-07-04'
      // }]
      for (let i = 0;i < this.batchattr.length;i++){
        if (this.batchattr[i].label == '生产日期'){
          this.batchattr[i].value = new Date(date)
          this.batchattr[i].value.setDate(date.getDate() - this.date1)
        }
      }
    },
    calculationDate2(date){
      for (let i = 0;i < this.batchattr.length;i++){
        if (this.batchattr[i].label == '截至日期'){
          this.batchattr[i].value = new Date(date)
          this.batchattr[i].value.setDate(date.getDate() + this.date1)
        }
      }
    }
  },
  activated(){
    this.init()
    // this.calculationDate1()
  },
  beforeRouteLeave(to, form, next){
    this.dialogFormVisible=false
    this.dialogFormVisible1=false
    this.dialogFormVisible2=false
    this.dialogFormVisible3=false
    next()
  }
}
</script>
<style lang="scss" scoped>
.el-form-item--mini span {
  font-size: 10px;
}
</style>